/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Admin
 */
@Entity
@Table(name = "Faculty")
@NamedQueries({
    @NamedQuery(name = "Faculty.findAll", query = "SELECT s FROM Faculty s"),
    @NamedQuery(name = "Faculty.findByFacultyId", query = "SELECT s FROM Faculty s WHERE s.id = :facultyId"),
    @NamedQuery(name = "Faculty.findByName", query = "SELECT s FROM Faculty s WHERE s.name = :name"),
    @NamedQuery(name = "Faculty.findByAddress", query = "SELECT s FROM Faculty s WHERE s.address = :address"),
    @NamedQuery(name = "Faculty.findByUnivesrity", query = "SELECT s FROM Faculty s WHERE s.university = :university")
})
public class Faculty implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)   
    @Column(name = "FacultyId")
    private Long id;
    
    @Column(name = "Name")
    private String name;
    
    @Column(name = "Address")
    private String address;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty")
    private List<Program> programs;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "faculty")
    private List<Subject> subjects;
    
    @ManyToMany(cascade = CascadeType.PERSIST, mappedBy = "faculties")
    private List<Professor> professors;
    
    @JoinColumn(name = "UniversityId", referencedColumnName = "UniversityId")
    @ManyToOne(optional = true, cascade= CascadeType.PERSIST)
    private University university;

    public Faculty() {
    }
    
    public Faculty(String name, String address){
        this.name = name;
        this.address = address;
    }
    
    public List<Professor> getProfessors() {
        return professors;
    }

    public void setProfessors(List<Professor> professors) {
        this.professors = professors;
    }

    public University getUniversity() {
        return university;
    }

    public void setUniversity(University university) {
        this.university = university;
    }

    public List<Program> getPrograms() {
        return programs;
    }

    public void setPrograms(List<Program> programs) {
        this.programs = programs;
    }

    public List<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(List<Subject> subjects) {
        this.subjects = subjects;
    }

    public Long getId() {
        return id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public void addSubject(Subject subject) {
        if(subjects == null){
            subjects = new ArrayList<Subject>();
        }
//        if (!subjects.contains(subject)) {
            subjects.add(subject);
//        }
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Faculty)) {
            return false;
        }
        Faculty other = (Faculty) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name + " (" + id + ")";
    }
    
}
